package com.wc.alorithm_blue_bridge._思维.食堂;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2025/6/5 18:41
 * @description
 * https://www.lanqiao.cn/problems/17004/learning/?page=1&first_category_id=1&name=%E9%A3%9F%E5%A0%82
 */
public class Main {
    /**
     * 思路；
     * 动态规划
     * f[i][j][k] 表示 买了前 i 种菜, 还缺 j 盘菜, 能够 赠送 k 盘菜
     * 那如何转移呢
     * 遍历 第 i 盘菜的时候, 假设买 x 盘, 缺 a = s[i] - x,
     * 可赠送 b = x / 2, 那么 x / 2 的部分是可以补充前面残缺的,
     * 剩下的是可以补充到赠送的, 假设补充 c 个到残缺的里面
     * f[i][j + a - c][k + b - c] = f[i][j][k] + x * p[i]
     */

    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 55, M = 310, K = 210, INF = 0x3f3f33f;
    static int[][][] f = new int[N][M][K];
    static int[] s = new int[N], p = new int[N];
    static int n;

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            n = sc.nextInt();
            int sum = 0;
            for (int i = 1; i <= n; i++) {
                p[i] = sc.nextInt();
                s[i] = sc.nextInt();
                sum += s[i];
            }

            for (int i = 0; i <= n; i++) {
                for (int j = 0; j <= sum; j++) {
                    for (int k = 0; k <= sum / 2; k++) {
                        f[i][j][k] = INF;
                    }
                }
            }
            f[0][0][0] = 0;
            sum = 0;
            for (int i = 1; i <= n; i++) {
                for (int a = 0; a <= sum; a++) {
                    int gift = (sum - a) / 2 + 1;
                    for (int b = 0; b <= gift; b++) {
                        for (int j = 0; j <= s[i]; j++) {
                            int g = j / 2, lose = s[i] - j;
                            int price = j * p[i];
                            for (int c = 0; c <= Math.min(g, a) + Math.min(lose, b); c++) {
                                f[i][a - c + lose][b + g - c] = Math.min(f[i][a - c + lose][b + g - c], f[i - 1][a][b] + price);
                            }
                        }
                    }
                }

                sum += s[i];
            }
            int res = INF;
            for (int i = 0; i <= sum; i++) res = Math.min(res, f[n][0][i]);
            out.println(res);
        }
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

